$(function () {
    $("#jqGrid").Grid({
        url: '../sparepartback/list',
        colModel: [
            {label: '退库单号', name: 'backCode', index: 'BACK_CODE', width: 80},
            {
                label: '退库日期', name: 'backDate', index: 'BACK_DATE', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm');
                }
            },
            {label: '领用单位', name: 'useUnit', index: 'USE_UNIT', width: 80},
            {label: '出库仓库', name: 'outStore', index: 'OUT_STORE', width: 80},
            {label: '仓库位置', name: 'postionName', index: 'postionName', width: 80},
            {label: '物料编码', name: 'partCode', index: 'PART_CODE', width: 80},
            {label: '名称', name: 'name', index: 'NAME', width: 80},
            {label: '规格', name: 'spec', index: 'SPEC', width: 80},
            {label: '退库数量', name: 'backNum', index: 'BACK_NUM', width: 80},
            {label: '备注', name: 'remark', index: 'REMARK', width: 80},
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
        ]
    });
});
// 校验可退库数方法
const validateSequence = (rule, value, callback) => {
    if (!Number.isInteger(+value)) {
        callback(new Error('输入数字'));
    } else{
        let backNum = parseInt(value);
        if(backNum <= 0){
            callback(new Error('退库数不得小于0！'));
        }else if(vm.sparePartOut.relNum < backNum){
            callback(new Error('退库数不得大于' + vm.sparePartOut.relNum));
        }else{
            callback();
        }
    }
};

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        sparePartBack: {outCode: ''},
        ruleValidate: {
            outCode: [
                {required: true, message: '出库单号不能为空', trigger: 'change'}
            ],
            backNum: [
                { required: true, validator: validateSequence, trigger: 'blur'}
            ]
        },
        q: {
            partName: '', // 备件名称
            spec: '', // 备件规格
            partCode: '', // 备件编码
            erpCode: '', // ERP编码
            producor:'' // 供应厂家
        },
        sparePartOut: {}
    },
    methods: {
        callBack(){
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.sparePartBack = {id: "", delFlag: 1, checkStatus: 0, backDate: new Date(), outCode: ''};
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.sparePartBack.id == (null || '') ? "../sparepartback/save" : "../sparepartback/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.sparePartBack),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            vm.getInfo(ids);
            confirm('确定要删除选中的记录？', function () {
                vm.sparePartBack.delFlag = 0;
                Ajax.request({
                    url: "../sparepartback/update",
                    params: JSON.stringify(vm.sparePartBack),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
            /*confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../sparepartback/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });*/
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../sparepartback/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.sparePartBack = r.sparePartBack;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: vm.q,
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                partName: '', // 备件名称
                spec: '', // 备件规格
                partCode: '', // 备件编码
                erpCode: '', // ERP编码
                producor:'' // 供应厂家
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        //显示出库列表
        getSparePartOut: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择出库单号',
                content: '../spare/sparePartOutSel.html',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getSparePartOut();
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        if(res.relNum === '0'){
                            alert("当前工单可退库数为0，请更换！");
                            return;
                        }
                        layer.close(index);
                        vm.sparePartOut = res;
                        vm.sparePartBack.outId = res.id;
                        vm.sparePartBack.outCode = res.outCode;
                        vm.sparePartBack.useUnit = res.useUnit;
                        vm.sparePartBack.outStoreId = res.outStoreId;
                        vm.sparePartBack.outStore = res.outStore;
                        vm.sparePartBack.partCode = res.partCode;
                        vm.sparePartBack.name = res.name;
                        vm.sparePartBack.spec = res.spec;
                        vm.sparePartBack.partUnit = res.partUnit;
                        vm.sparePartBack.relNum = res.relNum;
                        vm.sparePartBack.postionId = res.postionId;
                        vm.sparePartBack.postionName = res.cargoLocation;
                    }
                }
            })
        }
    }
});
